package deibay.server.commands;

import deibay.datamodel.Category;
import deibay.datamodel.Item;
import deibay.messages.Message;
import deibay.messages.NewItemMsg;
import deibay.messages.ReplyNewItemMsg;
import deibay.server.ServerManager;
import deibay.server.eventargs.NewItemEventArg;
import deibay.server.eventargs.SendMessageEventArg;
import deibay.server.events.NewItemEvent;
import deibay.server.events.SendMessageEvent;
import deibay.users.User;

public class NewItemCommand extends Command{

	public NewItemCommand(User user, Message msg) {
		super(user, msg);		
	}

	@Override
	public void execute() {
		NewItemMsg msg = (NewItemMsg)message;		
		Item item = new Item(msg.getName(), msg.getDescription(), user.getId(), new Category(null, msg.getCategory()), msg.getPrice(),  msg.getDate());
		ServerManager.getInstance().getEventManager().fireEvent(new NewItemEvent(new NewItemEventArg(item)));
		ServerManager.getInstance().getEventManager().fireEvent(new SendMessageEvent(new SendMessageEventArg(user.getId(), new ReplyNewItemMsg(ReplyNewItemMsg.class.getSimpleName(), true))));
	}

}
